package com.huike.pmps.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.huike.pmps.dao.MemberDao;
import com.huike.pmps.dao.PermissionDao;
import com.huike.pmps.model.constants.GlobalConstants;
import com.huike.pmps.model.entity.Member;
import com.huike.pmps.model.entity.Permission;
import com.huike.pmps.model.entity.Role;
import com.huike.pmps.model.vo.Result;
import com.huike.pmps.model.vo.security.MenuVo;
import com.huike.pmps.service.PermissionService;
import com.huike.pmps.utils.TreeMenusUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * 作者：谌贵斌(黑桃K)
 * 日期: 2888-08-08
 * 老师QQ: 272488352
 * 官方QQ交流群: 377748272
 * 刀客程序员官网：http://www.daoke360.com
 * 刀客程序员淘宝旗舰店：https://daoke360.taobao.com/
 */
@Service
public class PermissionServiceImpl implements PermissionService {
    @Autowired
    MemberDao memberDao;
    @Autowired
    PermissionDao permissionDao;

    /**
     * 将用户菜单权限生成树状
     *
     * @param memberId
     * @return
     */
    @Override
    public List<MenuVo> selectPermissionByMemberId(int memberId) {
        //获取所有系统资源
        QueryWrapper<Permission> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("state", GlobalConstants.SYSTEM_PERMISSION_ON);
        queryWrapper.orderByAsc("sort");
        //获取所有系统资源
        List<Permission> permissionList = permissionDao.selectList(queryWrapper);
        if (memberId != 1) {//系统超级管理员
            //系统一级菜单
            List<Integer> oneIds = new ArrayList<>();
            permissionList.forEach(p -> {
                if (p.getParentId() == 0) {
                    oneIds.add(p.getId());
                }
            });
            //系统二级菜单
            List<Integer> twoIds = new ArrayList<>();
            permissionList.forEach(p -> {
                if (oneIds.contains(p.getParentId())) {
                    twoIds.add(p.getId());
                }
            });

            //系统三级菜单
            List<Integer> threeIds = new ArrayList<>();
            permissionList.forEach(p -> {
                if (twoIds.contains(p.getParentId())) {
                    threeIds.add(p.getId());
                }
            });

            //用户所具有的权限
            Set<Permission> permissionSet = new HashSet<>();
            Member member = memberDao.findById(memberId);
            for (Role role : member.getRoles()) {
                permissionSet.addAll(role.getPermissions());
            }


            final Set<Integer> ids = new HashSet<>();

            permissionSet.forEach(p -> {
                //判断用户权限是否在三级菜单中
                if (threeIds.contains(p.getId())) {
                    //将三级菜单添加到ids
                    ids.add(p.getId());
                    //将三级菜单对应的二级菜单添加到ids
                    ids.add(p.getParentId());
                    //将二级菜单对应的一级菜单添加到ids中
                    permissionList.forEach(t -> {
                        if (t.getId() == p.getParentId()) ids.add(t.getId());
                    });
                }

                //判断用户权限是否在二级菜单中
                if (twoIds.contains(p.getId())) {
                    //将二级菜单添加到ids
                    ids.add(p.getId());
                    //将二级菜单对应的一级菜单添加到ids中
                    ids.add(p.getParentId());
                }

                //判断用户权限是否在一级菜单中
                if (twoIds.contains(p.getId())) {
                    //将一级级菜单添加到ids
                    ids.add(p.getId());
                }
            });
            List<Permission> pList = new ArrayList<>();
            permissionList.forEach(p -> {
                if (ids.contains(p.getId())) {
                    pList.add(p);
                }
            });
            return TreeMenusUtil.tree(pList);
        }else{
           return TreeMenusUtil.tree(permissionList);
        }
    }

}
